<?php
header("Content-Type:text/xml");
echo'<?xml version="1.0" encoding="utf-8" standalone="yes" ?>';
echo '<response>';
require_once('includes/include.php');
class blogDeletedao extends blogdao {
	public $log;
	public function __construct(){
		$this->log = new Log();
		parent::__construct();
	}
	/**
	 * get local images in the blog
	 *
	 * @param string $imgstring
	 * @return array
	 */
	function getLocalImages($imgstring){
		$imgstring = stripslashes($imgstring);
		$img_array = array();
		preg_match_all("/(src|SRC)=[\"|'| ]{0,}[\/]?(\w(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$imgstring,$img_array);
		$img_array = array_unique($img_array[2]);
		//$img_array = $img_array[2];
		return $img_array;	
	}
	public function goBlogDelete($blogId){
		if(!($this->deleteImages($blogId))){
			return false;
		}
		$sql = "delete from blog where id='$blogId'";
		$query = $this->queryit($sql);
		return $query;
	}
	
	public function deleteImages($blogId){
		$content = $this->getTargetBlogContent($blogId);
		$imagesPaths = $this->getLocalImages($content);
		if (!empty($imagesPaths)){
			foreach ($imagesPaths as $path){
				clearstatcache();
				$first = explode("/",$path);
				if ($first[0]=="connection"){
					$path=substr($path,1);
					$pathNew='';
					foreach ($first as $key=>$silp){
						if ($key==0)
							continue;
						if ($pathNew){
							$pathNew.="/".$silp;
						}else {
							$pathNew=$silp;
						}
						$path=$pathNew;
					}
					$this->log->write("###############".$path);
				}
				//$this->log->write("###############".$path);
				if (is_readable($path)){
					unlink($path);
				}else{
					$this->log->write("blog delete:image parth:".$path." can not be readed");
					//return false;
				}
			}
		}
		return true;
	}
	
}
$blogId = $_GET["blogId"];
if($blogId!=""){
	$dao = new blogDeletedao();
	$delete = $dao->goBlogDelete($blogId);
	if($delete){
		echo "ok";
	}
	else echo "failed";
}
echo '</response>';
?>